package cn.edu.xmu.oomall.order.dao;

import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.order.mapper.OrderPoMapper;
import cn.edu.xmu.oomall.order.model.bo.Order;
import cn.edu.xmu.oomall.order.model.po.OrderPo;
import cn.edu.xmu.oomall.order.model.po.OrderPoExample;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.cloneVo;

@Repository
public class OrderDao {
    @Autowired
    OrderPoMapper orderPoMapper;

    public ReturnObject<Order> getOrderById(Long id) {
        OrderPo orderPo;
        try {
            orderPo = orderPoMapper.selectByPrimaryKey(id);
        }
        catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }

        Order order = (Order) cloneVo(orderPo, Order.class);
        return new ReturnObject<>(order);
    }

    public ReturnObject getOrderState() {
        List<Map<String, Object>> stateList = new ArrayList<>();
        for (Order.State state : Order.State.values()) {
            Map<String, Object> temp = new HashMap<>();
            temp.put("code", state.getCode());
            temp.put("name", state.getDescription());
            stateList.add(temp);
        }
        return new ReturnObject<>(stateList);
    }

    public ReturnObject UpdateOrder(Long id, Order order) {
        int ret;
        try {
            OrderPo orderPo = orderPoMapper.selectByPrimaryKey(id);
            if (orderPo.getState() == Order.State.COMPLETED.getCode().byteValue()) {
                return new ReturnObject(ReturnNo.STATENOTALLOW, "当前状态禁止此操作");
            }
            else {
                orderPo.setAddress(order.getAddress());
                ret = orderPoMapper.updateByPrimaryKeySelective(orderPo);
            }
        }
        catch (Exception e) {
            return new ReturnObject<>(ReturnNo.FIELD_NOTVALID);
        }

        if (ret == 0) {
            return new ReturnObject<>(ReturnNo.FIELD_NOTVALID);
        }
        else return new ReturnObject();
    }

    public ReturnObject updateOrderState(Order order) {
        OrderPo orderPo = (OrderPo) cloneVo(order, OrderPo.class);
        int ret;
        try {
            ret = orderPoMapper.updateByPrimaryKeySelective(orderPo);
        }
        catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
        if (ret == 0) {
            return new ReturnObject(ReturnNo.FIELD_NOTVALID);
        }
        else return new ReturnObject();
    }

    public ReturnObject shopGetAllSimpleOrders(Long loginUser, Integer page, Integer pageSize) {
        OrderPoExample example = new OrderPoExample();
        example.setOrderByClause("gmtCreate DESC");
        OrderPoExample.Criteria criteria = example.createCriteria();
        List<OrderPo> orderPos = new ArrayList<>();
        PageHelper.startPage(page, pageSize);
        try {
            criteria.andCustomerIdEqualTo(loginUser);
            // criteria.andStateNotEqualTo(500);
            orderPos = orderPoMapper.selectByExample(example);
        }
        catch (DataAccessException e) {
            return new ReturnObject<>(ReturnNo.RESOURCE_ID_NOTEXIST);
        }
        return new ReturnObject<>(orderPos);
    }
}
